library(tidyverse)
library(purrrlyr)
library(lubridate)
library(forcats)
library(xts)
library(dygraphs)
library(plotly)
library(RColorBrewer)
library(themebg)

x <- dirr::get_rds("../data/tidy")

Questions

  1. Number of daily patients managed by pharmacy consult vs. traditional
  2. Which services are using consult service
  3. Which units are using consult service
  4. How does 2016 compare with 2015
  5. Indications for warfarin in each group
as.matrix(data_timeseries[, 2:3]) %>% 
    as.xts(order.by = data_timeseries$action.date, tzone = "US/Central") %>%
    dygraph(main = "Daily Warfarin Orders") %>%
    dySeries("pharmacy", label = "Pharmacy") %>%
    dySeries("traditional", label = "Traditional") %>%
    dyOptions(colors = brewer.pal(3, "Set1")) %>%
    dyRoller(rollPeriod = 5) %>%
    dyRangeSelector() %>%
    dyRangeSelector(c(max(data_timeseries$action.date) - months(12), max(data_timeseries$action.date)))
data_daily %>%
    ungroup() %>%
    filter(warfarin_day <= 10,
           med.datetime >= mdy("7/1/2016", tz = "US/Central"),
           med.datetime <= mdy("6/30/2017", tz = "US/Central")) %>%
    left_join(data_warfarin[c("millennium.id", "group", "initiation", "indication_group")], by = "millennium.id") %>%
    mutate_at("warfarin_day", as.integer) %>%
    mutate_at("warfarin_day", factor) %>%
    plot_ly(x = ~warfarin_day, y = ~med.dose, split = ~group) %>%
    add_boxplot() %>%
    layout(xaxis = list(title = "Day of therapy"),
           yaxis = list(title = "Warfarin dose (mg)"))
## Warning in ungroup_grouped_df(x): '.Random.seed' is not an integer vector
## but of type 'NULL', so ignored

Distribution of warfarin dose by day of therapy for FY17

gp <- ggplot(p, aes(x = warfarin_day, y = med.dose, color = group)) +
    geom_boxplot() +
    xlab("Day of therapy") +
    ylab("Warfarin dose (mg)") +
    scale_color_brewer("Group", palette = "Set1", labels = c("Pharmacy", "Traditional")) +
    theme_bg()

ggplotly(gp, dynamicTicks = TRUE)
data_daily %>%
    ungroup() %>%
    filter(warfarin_day <= 10,
           med.dose > 0,
           med.dose <= 15,
           !is.na(inr),
           med.datetime >= mdy("7/1/2016", tz = "US/Central"),
           med.datetime <= mdy("6/30/2017", tz = "US/Central")) %>%
    # dmap_at("warfarin_day", as.integer) %>%
    left_join(data_warfarin[c("millennium.id", "group", "initiation", "indication_group")], by = "millennium.id") %>%
    plot_ly() %>%
    add_markers(x = ~med.dose, y = ~inr, split = ~group, marker = list(symbol = "circle-open")) %>%
    layout(xaxis = list(title = "Warfarin dose (mg)"),
           yaxis = list(title = "INR"))

Relationship between warfarin dose and INR in FY17

#     
#     
#     ggplot(aes(x = med.dose, y = inr)) +
#     geom_point(aes(color = group), shape = 1) +
#     xlab("Warfarin dose (mg)") +
#     ylab("INR") +
#     scale_color_brewer("Group", palette = "Set1", labels = c("Pharmacy", "Traditional")) +
#     theme_bg()
# 
# ggplotly(p, dynamicTicks = TRUE)
data_warfarin %>%
    filter(warfarin_start >= mdy("7/1/2016", tz = "US/Central"),
           warfarin_start <= mdy("6/30/2017", tz = "US/Central")) %>%
    group_by(group) %>%
    summarize_if(is.logical, funs(mean(., na.rm = TRUE) * 100)) %>%
    gather(indication, val, afib:other) %>%
    arrange(group, desc(val)) %>%
    dmap_at("indication", fct_inorder) %>%
    plot_ly(x = ~indication, y = ~val, split = ~group) %>%
    add_bars() %>%
    layout(xaxis = list(title = "Indication"),
           yaxis = list(title = "Patients (%)"))
#     ggplot(aes(x = indication, y = val, fill = group)) +
#     geom_bar(stat = "identity", position = "dodge") +
#     xlab("Indication") +
#     ylab("Patients (%)") +
#     scale_fill_brewer("Group", palette = "Set1") +
#     theme_bg(xticks = FALSE)
# 
# ggplotly(p, dynamicTicks = TRUE)